package gen

const (
	Ego = "ego"

	Mysql      = "mysql"
	Postgresql = "postgresql"
)

const (
	modelOutPath = "productor/dao"
	apiOutPath   = "productor/api"
	srvOutPath   = "productor/service"
	cacheOutPath = "productor/redis"
)

type Opts struct {
	WithModel   bool
	WithCurd    bool
	WithRedis   bool
	WithService bool
	WithApi     bool
	Source      string
}

type Option func(opts *Opts)

func WithModel() Option {
	return func(opts *Opts) {
		opts.WithModel = true
	}
}
func WithCurd() Option {
	return func(opts *Opts) {
		opts.WithCurd = true
	}
}
func WithRedis() Option {
	return func(opts *Opts) {
		opts.WithRedis = true
	}
}
func WithService() Option {
	return func(opts *Opts) {
		opts.WithService = true
	}
}
func WithApi() Option {
	return func(opts *Opts) {
		opts.WithApi = true
	}
}

func WithSource(source string) Option {
	return func(opts *Opts) {
		switch source {
		case Mysql:
			opts.Source = Mysql
		default:
			opts.Source = Postgresql
		}
	}
}

type Gener struct {
	Table     string
	Module    string
	DSN       string
	Framework string
	Opts      Opts
}

func NewGener(table, module, dsn string, options ...Option) Gener {
	var opts Opts
	for _, opt := range options {
		opt(&opts)
	}

	return Gener{
		Table:     table,
		Module:    module,
		DSN:       dsn,
		Framework: Ego,
		Opts:      opts,
	}
}

func (g Gener) Generate() {
	if g.Opts.WithModel {
		// 生成结构体
		switch g.Opts.Source {
		case Mysql:
			GenModelMysql(g.DSN, g.Table)
		default:
			GenModel(g.DSN, g.Table)
		}
	}

	if g.Opts.WithCurd {
		// 生成curd
		GenCURD(g.DSN, g.Table, g.Module, g.Framework)
	}

	if g.Opts.WithApi {
		// 生成api
		GenApi(g.Table, g.Module, g.Framework)
	}

	if g.Opts.WithService {
		//生成service
		GenService(g.Table, g.Module, g.Framework)
	}

	if g.Opts.WithRedis {
		//生成redis
		GenRedis(g.Table, g.Module, g.Framework)
	}

}

func (g Gener) GenerateModel() {
	// 生成结构体
	GenModel(g.DSN, g.Table)
}

func (g Gener) GenerateApi() {
	// 生成ego的api
	GenApi(g.Table, g.Module, g.Framework)
}

func (g Gener) GenerateService() {
	//生成ego的service
	GenService(g.Table, g.Module, g.Framework)
}

func (g Gener) GenerateCurd() {
	// 生成ego的curd
	GenCURD(g.DSN, g.Table, g.Module, g.Framework)
}

func (g Gener) GenerateRedis() {
	// 生成ego的redis
	GenRedis(g.Table, g.Module, g.Framework)
}
